﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PhanMemDoVui.DataAccess;
using System.Windows.Forms;
using System.Data;
using PhanMemDoVui.BusinessObject;

namespace PhanMemDoVui.BusinessLogic
{
    class QuestionsBUS
    {
        private QuestnAns question;
        #region Constructor
        public QuestionsBUS()
        {
            question = new QuestnAns();
        }
#endregion
        #region ThemCauHoi
        //them cau hoi vao ngan hang Temp
        public bool ThemCauHoi(QuestnAns qa)
        {
            if (qa == null)
                return false;
            ConnectData conn = new ConnectData();
            int row = conn.DemSoRecord("QuestnAns") + 2;
            string insert = null;
            if (qa.CreatedBy != "")
                insert = string.Format("Insert into QuestnAns "
                    + "values (" + row.ToString() + ", N'{0}', N'{1}', N'{2}', N'{3}', N'{4}', {5}, '{6}', '{7}', 'Temp')", 
                    qa.Question, qa.Ans1, qa.Ans2, qa.Ans3, qa.Ans4, qa.RAns.ToString(), qa.Difficulty, qa.CreatedBy);
            else
                insert = string.Format("Insert into QuestnAns "
                    + "values (" + row.ToString() + ", N'{0}', N'{1}', N'{2}', N'{3}', N'{4}', {5}, '{6}', NULL, 'Temp')", 
                    qa.Question, qa.Ans1, qa.Ans2, qa.Ans3, qa.Ans4, qa.RAns.ToString(), qa.Difficulty);
            return conn.ExecuteNonQuery(insert);
        }
        #endregion
        #region CungCapDeThi
        //Cung cap mot so cau hoi ngau nhien theo so luong quy dinh
        public DataTable CungCapDeThi(int maExam, int numEasy, int numMode, int numHard, int numVhar)
        {
            ConnectData conn = new ConnectData();
            DataTable dt = new DataTable();
            string select = "Select * From (Select TOP " + numEasy.ToString() + " q1.*"
                + " From QuestnAns q1"
                + " Where q1.Difficulty = 'easy' and MaBank = 'Bank'"
                + " And q1.MaCH not in (Select MaCH From ExamUse Where Ma = " + maExam.ToString() + ")"
                + " ORDER BY NEWID()) as a"
                + " UNION "
                + "Select * From (Select TOP " + numMode.ToString() + " q2.*"
                + " From QuestnAns q2"
                + " Where q2.Difficulty = 'mode' and MaBank = 'Bank'"
                + " And q2.MaCH not in (Select MaCH From ExamUse Where Ma = " + maExam.ToString() + ")"
                + " ORDER BY NEWID()) as b"
                + " UNION "
                + "Select * From (Select TOP " + numHard.ToString() + " q3.*"
                + " From QuestnAns q3"
                + " Where q3.Difficulty = 'hard' and MaBank = 'Bank'"
                + " And q3.MaCH not in (Select MaCH From ExamUse Where Ma = " + maExam.ToString() + ")"
                + " ORDER BY NEWID()) as c"
                + " UNION "
                + "Select * From (Select TOP " + numVhar.ToString() + " q4.*"
                + " From QuestnAns q4"
                + " Where q4.Difficulty = 'vhar' and MaBank = 'Bank'"
                + " And q4.MaCH not in (Select MaCH From ExamUse Where Ma = " + maExam.ToString() + ")"
                + " ORDER BY NEWID()) as d";
            try
            {
                dt = conn.GetDataTable(select);
            }
            catch (System.Exception ex)
            {
            	MessageBox.Show("Lỗi: " + ex.ToString(), "QuizShow - FatalError", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            return dt;
        }
        #endregion
        #region ChuyenCauHoi
        public bool ChuyenCauHoi(QuestnAns question)
        {
            string sql = "Update QuestnAns Set MaBank = 'Bank' Where Question = N'" + question.Question.ToString() + "'"; 
            ConnectData conn = new ConnectData();
            return conn.ExecuteNonQuery(sql);
        }
        #endregion
        #region XoaCauHoi
        public bool XoaCauHoi(QuestnAns question)
        {
            string sql = "Delete from QuestnAns Where Question = N'" + question.Question.ToString() + "'";
            ConnectData conn = new ConnectData();
            return conn.ExecuteNonQuery(sql);
        }
        #endregion
    }
}
